package taller2.controller.listeners.evaluateMeButtons;

import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Random;

import taller2.interfaces.AlgorithmApplicator;
import taller2.interfaces.Graph;
import taller2.interfaces.Node;
import taller2.model.factorys.GraphFactory;
import taller2.model.graph.algorithms.DijkstraApplicator;
import taller2.utils.Constants.Colores;
import taller2.view.windows.supervisor.DijstraSupervisorWindow;

public class DijstraEvaluateMeButtonListener extends EvaluateMeButtonListener{

	public DijstraEvaluateMeButtonListener(AlgorithmApplicator aplicator) {
		super(aplicator);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		menu.unselectButtons();
		
		DijkstraApplicator djstr = (DijkstraApplicator)aplicator;
		String path = djstr.getStartingGraphPath();

		if(path != null){
			Graph g = GraphFactory.directedGgraphInstance();
			
			g.load(path);
			g.unselectNodes();
			g.unselectConnections();
		
			ArrayList<Node> nodes = g.getNodes();
			int cantNodes = nodes.size();
		
			if(cantNodes > 0){
				Node start = nodes.get(djstr.getStartingNodePosition());
				start.changeColor(Colores.purple);
		
				Random rand = new Random();
				int num = rand.nextInt(cantNodes);
				Node end = nodes.get(num);
				end.select();

				Graph sol = GraphFactory.directedGgraphInstance();
				String pathSol = djstr.getGraphSol(cantNodes-num);
				sol.load(pathSol);
				
				new DijstraSupervisorWindow(g,sol);
//				new DijstraSupervisorWindow(sol,g); // para hacer trampa
			}
		}

	}

}
